home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Games / Pentominoes 2.0 / Shell ƒ / other products window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-29  |  6.1 KB  |  251 lines  |  [TEXT/MMCC]

  1. #include "other products window.h"
  2. #include "environment.h"
  3. #include "menus.h"
  4. #include "memory layer.h"
  5. #include "file utilities.h"
  6. #include "main.h"
  7. #include "text layer.h"
  8. #include "generic window handlers.h"
  9. #include "window layer.h"
  10. #include "program globals.h"
  11. #include <Folders.h>
  12.  
  13. #define kGrowBoxSize        15
  14. #define PROGRAMS_LIST_NAME    "\pMP programs list"
  15. #define kListResourceID        151
  16. #define LIST_CREATOR        'ttxt'
  17. #define LIST_TYPE            'ttro'
  18.  
  19. static    Boolean GetListFromDisk(void);
  20. static    Boolean SetupNewListFile(short fileID);
  21. static    Boolean GetListFromResource(void);
  22. static    unsigned long GetModificationDate(FSSpec *theFS);
  23. static    void PutProductListIntoTE(WindowRef theWindow);
  24.  
  25. static    Handle            gTheList=0L;
  26. static    TEClickLoopUPP    gClickLoopUPP;
  27. static    Boolean            gSetupDone=FALSE;
  28.  
  29. void SetupTheOtherProductsWindow(WindowRef theWindow)
  30. {
  31.     unsigned char    *titleStr="\pOther products";
  32.     Point            topLeft;
  33.     
  34.     SetWindowHeight(theWindow,
  35.         qd.screenBits.bounds.bottom-qd.screenBits.bounds.top-LMGetMBarHeight()-28);
  36.     SetWindowWidth(theWindow, qd.screenBits.bounds.right-qd.screenBits.bounds.left-70);
  37.     SetWindowAttributes(theWindow, kHasCloseBoxMask+kHasZoomBoxMask+kHasGrowBoxMask+kHasDocumentTitlebarMask);
  38.     topLeft.v=qd.screenBits.bounds.top+LMGetMBarHeight()+20;
  39.     topLeft.h=qd.screenBits.bounds.left+10;
  40.     SetWindowTopLeft(theWindow, topLeft);
  41.     SetWindowMaxDepth(theWindow, 1);
  42.     SetWindowAutoCenter(theWindow, FALSE);
  43.     SetWindowTitle(theWindow, titleStr);
  44.     SetWindowIsPrintable(theWindow, TRUE);
  45.     SetWindowIsZoomable(theWindow, TRUE);
  46.     
  47.     if (gSetupDone)
  48.         return;
  49.     
  50.     gSetupDone=TRUE;
  51.     
  52.     if (!GetListFromDisk())
  53.         GetListFromResource();
  54.     
  55.     gClickLoopUPP=NewTEClickLoopProc(MyClikLoop);
  56.     gNeedToOpenWindow=FALSE;
  57. }
  58.  
  59. void ShutDownTheOtherProductsWindow(void)
  60. {
  61.     if (gClickLoopUPP!=0L)
  62.         DisposeRoutineDescriptor(gClickLoopUPP);
  63. }
  64.  
  65. void OpenTheOtherProductsWindow(WindowRef theWindow)
  66. {
  67.     TEHandle        hTE;
  68.     FontInfo        theFontInfo;
  69.     Rect            vScrollBarRect, hScrollBarRect;
  70.     Rect            destRect, viewRect;
  71.     
  72.     hTE=GetWindowTE(theWindow);
  73.     if (hTE==0L)
  74.     {
  75.         SetRect(&vScrollBarRect, GetWindowWidth(theWindow)-kGrowBoxSize, -1,
  76.             GetWindowWidth(theWindow)+1, GetWindowHeight(theWindow)+1-kGrowBoxSize);
  77.         SetRect(&hScrollBarRect, -1, GetWindowHeight(theWindow)-kGrowBoxSize,
  78.             GetWindowWidth(theWindow)-kGrowBoxSize+1, GetWindowHeight(theWindow)+1);
  79.         SetWindowVScrollBar(theWindow,
  80.             NewControl(theWindow, &vScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0));
  81.         SetWindowHScrollBar(theWindow,
  82.             NewControl(theWindow, &hScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0));
  83.         
  84.         GetTERect(theWindow, &destRect, TRUE);
  85.         viewRect=destRect;
  86.         hTE=TENew(&destRect, &viewRect);
  87.         SetWindowTE(theWindow, hTE);
  88.         TextFont((**hTE).txFont=9840);
  89.         TextSize((**hTE).txSize=10);
  90.         TextFace((**hTE).txFace=0);
  91.         GetFontInfo(&theFontInfo);
  92.         (**hTE).fontAscent=theFontInfo.ascent;
  93.         (**hTE).lineHeight=theFontInfo.ascent+theFontInfo.descent+theFontInfo.leading;
  94.         AdjustViewRect(hTE);
  95.         TEAutoView(TRUE, hTE);
  96.         (**hTE).clickLoop=gClickLoopUPP;
  97.         PutProductListIntoTE(theWindow);
  98.     }
  99.     
  100.     SetWindowIsActive(theWindow, TRUE);
  101.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  102.     AdjustMenus();
  103. }
  104.  
  105. void DisposeTheOtherProductsWindow(WindowRef theWindow)
  106. {
  107.     TEHandle        hTE;
  108.     
  109.     hTE=GetWindowTE(theWindow);
  110.     if (hTE!=0L)
  111.     {
  112.         TEDispose(hTE);
  113.         SetWindowTE(theWindow, 0L);
  114.     }
  115. }
  116.  
  117. /* --------------------------------------------------------- */
  118. /* the rest of these are internal to other products window.c */
  119.  
  120. static    void PutProductListIntoTE(WindowRef theWindow)
  121. {
  122.     long            listEOF;
  123.     
  124.     if (gTheList==0L)
  125.         return;
  126.     
  127.     listEOF=GetHandleSize(gTheList);
  128.     HLock(gTheList);
  129.     SetTheText(theWindow, *gTheList, listEOF);
  130.     HUnlock(gTheList);
  131.     TESetSelect(0, 0, GetWindowTE(theWindow));
  132. }
  133.  
  134. static    Boolean GetListFromDisk(void)
  135. {
  136.     Boolean            isNewFile;
  137.     unsigned char    *name=PROGRAMS_LIST_NAME;
  138.     OSErr            isHuman;
  139.     short            vRefNum;
  140.     long            dirID;
  141.     FSSpec            listFileFS, progFS;
  142.     short            listFile;
  143.     long            listEOF;
  144.     unsigned long    diskListModDate, ourListModDate;
  145.     
  146.     isNewFile=FALSE;
  147.     /* find vRefNum and dirID of preferences folder, creating it if necessary */
  148.     isHuman=FindFolder(kOnSystemDisk, 'pref', kCreateFolder, &vRefNum, &dirID);
  149.     
  150.     if (isHuman!=noErr)        /* screwed up already?!? */
  151.         return FALSE;
  152.     
  153.     isHuman=FSMakeFSSpec(vRefNum, dirID, name, &listFileFS);    /* make FSSpec out of it */
  154.     if (isHuman!=noErr)
  155.     {
  156.         if (isHuman==fnfErr)    /* FSSpec is valid, but list file does not exist */
  157.         {
  158.             isHuman=FSpCreate(&listFileFS, LIST_CREATOR, LIST_TYPE, 0);    /* so create it */
  159.             if (isHuman!=noErr)                                        /* or not */
  160.                 return FALSE;
  161.             isNewFile=TRUE;        /* signal that list file is new */
  162.         }
  163.         else return FALSE;
  164.     }
  165.     isHuman=FSpOpenDF(&listFileFS, fsRdWrPerm, &listFile);    /* open list file */
  166.     if (isHuman!=noErr)
  167.         return FALSE;
  168.     
  169.     diskListModDate=GetModificationDate(&listFileFS);
  170.     GetApplicationFSSpec(&progFS);
  171.     ourListModDate=GetModificationDate(&progFS);
  172.     if (diskListModDate<ourListModDate)
  173.         isNewFile=TRUE;
  174.     
  175.     if (isNewFile)
  176.     {
  177.         if (!SetupNewListFile(listFile))
  178.         {
  179.             FSClose(listFile);
  180.             FSpDelete(&listFileFS);
  181.             return FALSE;
  182.         }
  183.         SetModificationDate(&listFileFS, ourListModDate);
  184.     }
  185.     else
  186.     {
  187.         GetEOF(listFile, &listEOF);
  188.         gTheList=NewHandle(listEOF);
  189.         if (gTheList==0L)
  190.         {
  191.             FSClose(listFile);
  192.             FSpDelete(&listFileFS);
  193.             return FALSE;
  194.         }
  195.         HLock(gTheList);
  196.         SetFPos(listFile, 1, 0L);
  197.         if (FSRead(listFile, &listEOF, *gTheList)!=noErr)
  198.         {
  199.             FSClose(listFile);
  200.             FSpDelete(&listFileFS);
  201.             DisposeHandle(gTheList);
  202.             gTheList=0L;
  203.             return FALSE;
  204.         }
  205.         HUnlock(gTheList);
  206.     }
  207.     
  208.     FSClose(listFile);
  209.     
  210.     return TRUE;
  211. }
  212.  
  213. static    Boolean SetupNewListFile(short fileID)
  214. {
  215.     long            listEOF;
  216.     
  217.     if (!GetListFromResource())
  218.         return FALSE;
  219.     
  220.     listEOF=GetHandleSize(gTheList);
  221.     if (SetEOF(fileID, listEOF)!=noErr)
  222.         return FALSE;
  223.     
  224.     SetFPos(fileID, 1, 0L);
  225.     HLock(gTheList);
  226.     if (FSWrite(fileID, &listEOF, *gTheList)!=noErr)
  227.         return FALSE;
  228.     HUnlock(gTheList);
  229.     
  230.     return TRUE;
  231. }
  232.  
  233. static    Boolean GetListFromResource(void)
  234. {
  235.     gTheList=(Handle)Get1Resource('TEXT', kListResourceID);
  236.     if (gTheList==0L)
  237.         return FALSE;
  238.     if (*gTheList==0L)
  239.         LoadResource(gTheList);
  240.     if (*gTheList==0L)
  241.     {
  242.         DisposeHandle(gTheList);
  243.         gTheList=0L;
  244.         return FALSE;
  245.     }
  246.     
  247.     DetachResource(gTheList);
  248.     
  249.     return TRUE;
  250. }
  251.